package com.heima.behavior.service.impl;

import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApCollectionBehaviorService;
import com.heima.common.constants.article.HotArticleConstants;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.CollectionBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.NewBehaviorDTO;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class ApCollectionBehaviorServiceImpl implements ApCollectionBehaviorService {
    //注入mongodb 模板对象
    @Autowired
    MongoTemplate mongoTemplate;

    //注入行为实体查询
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    RabbitTemplate rabbitTemplate;
    /**
     * 收藏行为
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult collectBehavior(CollectionBehaviorDTO dto) {
        //校验参数 需要登录 文章id不能null  操作类型 0 或 1
        Integer uid = AppThreadLocalUtils.getUser().getId();
        if (uid == null) {
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }

        //根据userId查询行为实体数据
        ApBehaviorEntry entry = apBehaviorEntryService.findByUserIdOrEquipmentId(uid, dto.getEquipmentId());
        if (entry == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }

        //如果是收藏操作 判断是否已经收藏
        if (dto.getOperation().shortValue() == 0) {
            //如果未收藏 新增收藏行为
            Query query = Query.query(Criteria.where("operation").is(0));
            ApCollection apCollection = mongoTemplate.findOne(query, ApCollection.class);
            ApCollection collection = new ApCollection();

            if (apCollection == null) {
                //如果未收藏 新增收藏行为
                collection.setEntryId(entry.getId());
                collection.setArticleId(dto.getArticleId());
                collection.setType((short) 0);
                collection.setCollectionTime(new Date());
                mongoTemplate.insert(collection);
            }
        }
        //如果是取消收藏操作  删除收藏行为
        if (dto.getOperation().shortValue() == 1) {
            Query query = Query.query(Criteria.where("type").is(0)
                    .and("articleId").is(dto.getArticleId()));
            mongoTemplate.remove(query, ApCollection.class);
        }

        //创建mess对象
        NewBehaviorDTO articleMess = new NewBehaviorDTO();
        //设置类型为收藏 COLLECTION
        articleMess.setType(NewBehaviorDTO.UpdateArticleType.COLLECTION);
        //设置文章id
        articleMess.setArticleId(dto.getArticleId());
        //判断是否收藏 收藏 add+1  取消收藏 add-1
        articleMess.setAdd(dto.getOperation() == 0 ? 1 : -1);
        rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE,JSON.toJSONString(articleMess));
        //点赞发送消息
        log.info("点赞行为发送消息 ===> {}", JSON.toJSONString(articleMess));
        //返回结果
        return ResponseResult.okResult();
    }
}
